package com.example.negativescreendemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.negativescreendemo.ActionItem;
import com.example.negativescreendemo.CardItem;
import com.example.negativescreendemo.NewsItem;
import com.example.negativescreendemo.R;
import com.example.negativescreendemo.ScheduleItem;
import com.example.negativescreendemo.TodoItem;

import java.util.List;

/**
 * @author: njb
 * @date: 2025/8/20 21:19
 * @desc: 描述
 */
public class NegativeScreenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<CardItem> cardItems;
    private OnItemClickListener listener;

    // 卡片类型
    public enum CardType {
        ACTIONS, SCHEDULE, NEWS, TODO
    }

    public NegativeScreenAdapter(Context context, List<CardItem> cardItems) {
        this.context = context;
        this.cardItems = cardItems;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);

        switch (viewType) {
            case 0: // 快捷功能卡片
                View actionView = inflater.inflate(R.layout.item_action_card, parent, false);
                return new ActionCardViewHolder(actionView);
            case 1: // 日程卡片
                View scheduleView = inflater.inflate(R.layout.item_schedule_card, parent, false);
                return new ScheduleCardViewHolder(scheduleView);
            case 2: // 新闻卡片
                View newsView = inflater.inflate(R.layout.item_news_card, parent, false);
                return new NewsCardViewHolder(newsView);
            case 3: // 待办事项卡片
                View todoView = inflater.inflate(R.layout.item_todo_card, parent, false);
                return new TodoCardViewHolder(todoView);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CardItem item = cardItems.get(position);

        if (holder instanceof ActionCardViewHolder) {
            ((ActionCardViewHolder) holder).title.setText(item.getTitle());
            ActionAdapter actionAdapter = new ActionAdapter(context, item.getActionItems());
            ((ActionCardViewHolder) holder).recyclerView.setAdapter(actionAdapter);
            ((ActionCardViewHolder) holder).recyclerView.setLayoutManager(
                    new GridLayoutManager(context, 3));
        }
        else if (holder instanceof ScheduleCardViewHolder) {
            ((ScheduleCardViewHolder) holder).title.setText(item.getTitle());
            ScheduleAdapter scheduleAdapter = new ScheduleAdapter(context, item.getScheduleItems());
            ((ScheduleCardViewHolder) holder).recyclerView.setAdapter(scheduleAdapter);
        }
        else if (holder instanceof NewsCardViewHolder) {
            ((NewsCardViewHolder) holder).title.setText(item.getTitle());
            NewsAdapter newsAdapter = new NewsAdapter(context, item.getNewsItems());
            ((NewsCardViewHolder) holder).recyclerView.setAdapter(newsAdapter);
        }
        else if (holder instanceof TodoCardViewHolder) {
            ((TodoCardViewHolder) holder).title.setText(item.getTitle());
            TodoAdapter todoAdapter = new TodoAdapter(context, item.getTodoItems());
            ((TodoCardViewHolder) holder).recyclerView.setAdapter(todoAdapter);
        }

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cardItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        CardType type = cardItems.get(position).getType();
        switch (type) {
            case ACTIONS: return 0;
            case SCHEDULE: return 1;
            case NEWS: return 2;
            case TODO: return 3;
            default: return 0;
        }
    }

    // 快捷功能卡片ViewHolder
    public static class ActionCardViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recyclerView;

        public ActionCardViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.card_title);
            recyclerView = itemView.findViewById(R.id.action_recycler);
        }
    }

    // 日程卡片ViewHolder
    public static class ScheduleCardViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recyclerView;

        public ScheduleCardViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.card_title);
            recyclerView = itemView.findViewById(R.id.schedule_recycler);
        }
    }

    // 新闻卡片ViewHolder
    public static class NewsCardViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recyclerView;

        public NewsCardViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.card_title);
            recyclerView = itemView.findViewById(R.id.news_recycler);
        }
    }

    // 待办事项卡片ViewHolder
    public static class TodoCardViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recyclerView;

        public TodoCardViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.card_title);
            recyclerView = itemView.findViewById(R.id.todo_recycler);
        }
    }

    // 快捷功能子项适配器
    public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ViewHolder> {
        private Context context;
        private List<ActionItem> items;

        public ActionAdapter(Context context, List<ActionItem> items) {
            this.context = context;
            this.items = items;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_action, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ActionItem item = items.get(position);
            holder.icon.setImageResource(item.getIconRes());
            holder.name.setText(item.getName());
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView icon;
            TextView name;

            public ViewHolder(View itemView) {
                super(itemView);
                icon = itemView.findViewById(R.id.action_icon);
                name = itemView.findViewById(R.id.action_name);
            }
        }
    }

    // 日程子项适配器
    public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ViewHolder> {
        private Context context;
        private List<ScheduleItem> items;

        public ScheduleAdapter(Context context, List<ScheduleItem> items) {
            this.context = context;
            this.items = items;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_schedule, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ScheduleItem item = items.get(position);
            holder.time.setText(item.getTime());
            holder.title.setText(item.getTitle());
            holder.location.setText(item.getLocation());
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView time, title, location;

            public ViewHolder(View itemView) {
                super(itemView);
                time = itemView.findViewById(R.id.schedule_time);
                title = itemView.findViewById(R.id.schedule_title);
                location = itemView.findViewById(R.id.schedule_location);
            }
        }
    }

    // 新闻子项适配器
    public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
        private Context context;
        private List<NewsItem> items;

        public NewsAdapter(Context context, List<NewsItem> items) {
            this.context = context;
            this.items = items;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_news, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            NewsItem item = items.get(position);
            holder.title.setText(item.getTitle());
            holder.source.setText(item.getSource());
            holder.time.setText(item.getTime());
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView title, source, time;

            public ViewHolder(View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.news_title);
                source = itemView.findViewById(R.id.news_source);
                time = itemView.findViewById(R.id.news_time);
            }
        }
    }

    // 待办事项子项适配器
    public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.ViewHolder> {
        private Context context;
        private List<TodoItem> items;

        public TodoAdapter(Context context, List<TodoItem> items) {
            this.context = context;
            this.items = items;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_todo, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            TodoItem item = items.get(position);
            holder.content.setText(item.getContent());
            holder.checkBox.setChecked(item.isCompleted());

            holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
                    item.setCompleted(isChecked));
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            CheckBox checkBox;
            TextView content;

            public ViewHolder(View itemView) {
                super(itemView);
                checkBox = itemView.findViewById(R.id.todo_checkbox);
                content = itemView.findViewById(R.id.todo_content);
            }
        }
    }

    // 点击事件接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
